package com.work.demo.idempotent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisIdempotence implements Idempotence {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean check(String idempotenceId) {
        return Boolean.TRUE.equals(redisTemplate.hasKey(idempotenceId));
    }

    @Override
    public void record(String idempotenceId) {
        redisTemplate.boundValueOps(idempotenceId).set("1");
    }

    @Override
    public void record(String idempotenceId, Integer time) {
        redisTemplate.boundValueOps(idempotenceId).set("1", time);

    }

    @Override
    public void delete(String idempotenceId) {
        redisTemplate.delete(idempotenceId);
    }
}
